/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author jacopo
 */
@Entity
@Table(name = "WeatherInformation")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "WeatherInformation.findAll", query = "SELECT w FROM WeatherInformation w"),
    @NamedQuery(name = "WeatherInformation.findByDayTime", query = "SELECT w FROM WeatherInformation w WHERE w.weatherInformationPK.dayTime = :dayTime"),
    @NamedQuery(name = "WeatherInformation.findByLocation", query = "SELECT w FROM WeatherInformation w WHERE w.weatherInformationPK.location = :location"),
    @NamedQuery(name = "WeatherInformation.findByUrlImage", query = "SELECT w FROM WeatherInformation w WHERE w.urlImage = :urlImage"),
    @NamedQuery(name = "WeatherInformation.findByDetails", query = "SELECT w FROM WeatherInformation w WHERE w.details = :details")})
public class WeatherInformation implements Serializable {
    @Column(name = "ConditionCode")
    private Integer conditionCode;
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected WeatherInformationPK weatherInformationPK;
    @Size(max = 120)
    @Column(name = "UrlImage")
    private String urlImage;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "Details")
    private String details;
    @ManyToMany(mappedBy = "weatherInformationCollection")
    private Collection<Event> eventCollection;

    public WeatherInformation() {
    }
    
    public WeatherInformation(WeatherInformationPK weatherInformationPK) {
        this.weatherInformationPK = weatherInformationPK;
    }

    public WeatherInformation(WeatherInformationPK weatherInformationPK, String details) {
        this.weatherInformationPK = weatherInformationPK;
        this.details = details;
    }
    
    public WeatherInformation(Date dayTime, String location) {
        this.weatherInformationPK = new WeatherInformationPK(dayTime, location);
    }

     public WeatherInformationPK getWeatherInformationPK() {
        return weatherInformationPK;
    }

    public void setWeatherInformationPK(WeatherInformationPK weatherInformationPK) {
        this.weatherInformationPK = weatherInformationPK;
    }

    public String getUrlImage() {
        return urlImage;
    }

    public void setUrlImage(String urlImage) {
        this.urlImage = urlImage;  
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    @XmlTransient
    public Collection<Event> getEventCollection() {
        return eventCollection;
    }

    public void setEventCollection(Collection<Event> eventCollection) {
        this.eventCollection = eventCollection;
    }
    
    public void addEventToCollection(Event event){
        if(this.eventCollection == null){
            this.eventCollection = new ArrayList<>();
        }
        if(!this.eventCollection.contains(event)){
            this.eventCollection.add(event);
        }
    }
    
    public void removeEventFromCollection(Event event){
        if(this.eventCollection == null)
            return;
        this.eventCollection.remove(event);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (weatherInformationPK != null ? weatherInformationPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof WeatherInformation)) {
            return false;
        }
        WeatherInformation other = (WeatherInformation) object;
        if ((this.weatherInformationPK == null && other.weatherInformationPK != null) || (this.weatherInformationPK != null && !this.weatherInformationPK.equals(other.weatherInformationPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
       
        String r = String.format("Forecast: %s on date: %s in: %s with code: %d icon: %s", this.details, this.weatherInformationPK.getDayTime(), this.weatherInformationPK.getLocation(),this.conditionCode, this.urlImage);
        return r ;
    }

    public Integer getConditionCode() {
        return conditionCode;
    }

    public void setConditionCode(Integer conditionCode) {
        this.conditionCode = conditionCode;
    }
    
}
